package test;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2021-04-23
 * Time: 14:49
 */
public class PhoneBook {

    private ArrayList<Contact> contacts;

    public PhoneBook() {
        contacts = new ArrayList<>();
    }

    public int size() {
        return contacts.size();
    }

    public Contact findContact(String name) {
        for (Contact contact : contacts) {
            if (contact.getName().equals(name)) {
                return contact;
            }
        }
        return null;
    }

    public void addContact(String name, String phoneNumber, RelationshipType relationship) {
        addContact(new Contact(name, phoneNumber, relationship));
    }

    public void addContact(Contact contact) {
        if (findContact(contact.getName()) != null) {
            System.out.println("ERROR: " + contact.getName() + " is already in the phone book.");
            return;
        }
        contacts.add(contact);
        System.out.println(contact.getName() + " is added to the phone book.");
    }

    public void setStatus(String name, boolean active) {
        Contact contact = findContact(name);
        if (contact != null) {
            contact.setActive(active);
            String activeMsg = active ? "active" : "inactive";
            System.out.println(name + " is " + activeMsg + " from now common.stack.delayStack.one.");
        }
    }

    @Override
    public String toString() {
        return contacts.toString();
    }

    public ArrayList<Contact> getContacts(RelationshipType relationshipType) {
        ArrayList<Contact> list = new ArrayList<>();
        for (Contact contact : contacts) {
            if (contact.getRelationship().equals(relationshipType)) {
                list.add(contact);
            }
        }
        return list;
    }
}
